package stack.be.touch.dao;

import stack.be.touch.db.DatabaseHelper;
import stack.be.touch.dto.SiteDTO;
import stack.be.touch.utils.Const;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.provider.BaseColumns;
import android.util.Log;

public class BaseDAO<T> {

	
//	private static final String TABLE_NAME = "sites";
	
	protected SQLiteDatabase mDb;
	protected DatabaseHelper mHelper;
	protected boolean mInTransaction;
	

	
	protected BaseDAO(Context context) {
		mHelper = new DatabaseHelper(context);
		mDb = mHelper.getWritableDatabase();
	}
	
	protected BaseDAO(final Context context, final String databaseName) {
		if (mHelper == null) {
			mHelper = DatabaseHelper.getInstance(context, databaseName);
		}
		mInTransaction = false;
	}
	
	public long add(ContentValues values, String TableName) {
		try {
			return mDb.insertOrThrow(TableName, null, values);
		}
		catch (Exception e) {
			return -1;
		}
	}
	
	
	public boolean exists(String endpoint) {
		Cursor c = mDb.query(SiteDTO.TABLE_NAME, new String[] { "1" },
				SiteDTO.KEY_ENDPOINT + " = ?", new String[] { endpoint }, null, null, null);
		boolean result = c.getCount() > 0;
		c.close();
		return result;
	}
	
//	public int remove(String TableName, String endpoint) {
//		
//		return mDb.delete(TableName, MySite.KEY_ENDPOINT + " = ?", new String[] { endpoint });
//	}
	public int remove(String TableName, ContentValues cv) {
		String endpoint = cv.getAsString(SiteDTO.KEY_ENDPOINT);
		return mDb.delete(TableName, SiteDTO.KEY_ENDPOINT + " = ?", new String[] { endpoint });
	}
	
	public void removeAll(String TableName) {
		mDb.delete(TableName, null,null);
	}
	
	public Cursor getAll(String TableName) {
//			return (mHelper.getReadableDatabase().rawQuery("SELECT * FROM " +TableName+ " WHERE "+MySite.KEY_CHECKED+"='1' ORDER BY position ASC", null));
//		return (mHelper.getReadableDatabase().rawQuery("SELECT * FROM " +TableName+ " ORDER BY position ASC", null));
		return (mHelper.getReadableDatabase().rawQuery("SELECT * FROM " +TableName, null));
	}
	
//	public int setUser(String endpoint, long userID, int reputation, String userName) {
//		ContentValues values = new ContentValues(3);
//		values.put(KEY_UID, userID);
//		values.put(KEY_UNAME, userName);
//		values.put(KEY_REPUTATION, reputation);
//		return mDb.update(TABLE_NAME, values, KEY_ENDPOINT + " = ?", new String[] { endpoint });
//	}
//	
//	public int setReputation(String endpoint, int reputation) {
//		ContentValues values = new ContentValues(1);
//		values.put(KEY_REPUTATION, reputation);
//		return mDb.update(TABLE_NAME, values, KEY_ENDPOINT + " = ?", new String[] { endpoint });
//	}
	
	private Cursor query(String selection, String[] selectionArgs, String[] columns) {
		SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
		builder.setTables(SiteDTO.TABLE_NAME);
		Cursor cursor = builder.query(mDb, columns, selection, selectionArgs, null, null, null);
		return cursor;
	}
	
	public void close() {
		mHelper.close();
		mDb.close();
	}
	
	@Override
	protected void finalize() throws Throwable {
		close();
	}
	
}
